package xyz.robinbingo.pfboot.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.robinbingo.pfboot.entity.Car;
import xyz.robinbingo.pfboot.entity.lazymodel.LazyModelCar;
import xyz.robinbingo.pfboot.repository.CarRepository;
import xyz.robinbingo.pfboot.util.FaceUtils;

import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.List;

/**
 * CarListController
 *
 * @author Robin
 * @date 2018/10/18 11:00
 **/
@Named
@ViewScoped
public class CarListController {

    @Autowired
    private CarRepository carRepository;

    @Autowired
    private LazyModelCar cars;
    private List<Car> selectedCars;

    public LazyModelCar getCars() {
        return cars;
    }

    public List<Car> getSelectedCars() {
        return selectedCars;
    }

    public void setSelectedCars(List<Car> selectedCars) {
        this.selectedCars = selectedCars;
    }

    /**
     * 删除数据
     *
     * @author Robin
     * @date 2018/10/19 17:04
     **/
    public void delete() {
        if (CollectionUtil.isEmpty(selectedCars)) {
            FaceUtils.addMessage("请选择要删除的数据", FacesMessage.SEVERITY_WARN);
            return;
        }
        int numCars = selectedCars.size();
        carRepository.deleteAll(selectedCars);
        selectedCars.clear();
        FaceUtils.addMessage("成功删除" + numCars + "条数据");
    }

}
